//
//  MeViewController.m
//  KiklinkApp
//
//  Created by LJC on 14-9-3.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "MeViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"
#import "UIImageView+WebCache.h"

#import "MessageViewController.h"
#import "UnpayViewController.h"
#import "PaylistViewController.h"
#import "EvaluateViewController.h"
#import "CollectionViewController.h"
#import "CouponViewController.h"
#import "SettingsViewController.h"

#import "AccountManager.h"

@interface MeViewController ()
{
    ImagesManager *imagesManager;
}
@end

@implementation MeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"我的";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    
    //BundleImages
    imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageButton setFrame:CGRectMake(0, 0, NavBarHeight/2.4, NavBarHeight/2.5)];
    [messageButton setBackgroundImage:[imagesManager message] forState:UIControlStateNormal];
    [messageButton setBackgroundImage:[imagesManager messageTapped] forState:UIControlStateHighlighted];
    [messageButton addTarget:self action:@selector(messageAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:messageButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //rightNavItem
    UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsButton setFrame:CGRectMake(0, 0, NavBarHeight/2.4, NavBarHeight/2.5)];
    [settingsButton setBackgroundImage:[imagesManager settings] forState:UIControlStateNormal];
    [settingsButton setBackgroundImage:[imagesManager settingsTapped] forState:UIControlStateHighlighted];
    [settingsButton addTarget:self action:@selector(settingsAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:settingsButton];
    self.navigationItem.rightBarButtonItem = rightNavBarItem;
    
    
    //loginView
    self.portrait.contentMode = UIViewContentModeScaleAspectFill;
    self.portrait.clipsToBounds = YES;
    
    [self.unloginView.layer setMasksToBounds:YES];
    [self.unloginView.layer setCornerRadius:5];
    [self.unloginView.layer setBorderWidth:1.5];
    [self.unloginView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [self.unloginView addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.unloginView setShowsTouchWhenHighlighted:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    LoginStatus = [AccountManager isKiklinkLoggedin];
    
    if (LoginStatus == YES) {
        self.unloginView.hidden = YES;
        self.loginView.hidden = NO;
        [self.portrait sd_setImageWithURL:[NSURL URLWithString:[Base_Url stringByAppendingString:[[AccountManager accountInfo] objectForKey:KEY_ICON_URL]]] placeholderImage:[UIImage imageNamed:@"portrait.png"]];
        self.userName.text = [[AccountManager accountInfo] objectForKey:KEY_DISPLAY_NAME];
    } else {
        self.unloginView.hidden = NO;
        self.loginView.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - login
- (void)login
{
    [AccountManager openLoginUI];
}
#pragma mark - login End

#pragma mark - messageAction
- (void)messageAction
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
        MessageViewController *messageVC = [[MessageViewController alloc] init];
        [self.navigationController pushViewController:messageVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - messageAction End

#pragma mark - settingsAction
- (void)settingsAction
{
    SettingsViewController *settingsVC = [[SettingsViewController alloc] init];
    [self.navigationController pushViewController:settingsVC animated:YES];
}
#pragma mark - settingsAction End

#pragma mark - unPayAction
- (IBAction)unPayAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
//        UnpayViewController *unpayVC = [[UnpayViewController alloc] init];
//        [self.navigationController pushViewController:unpayVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - unPayAction End

#pragma mark - payListAction
- (IBAction)payListAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
//        PaylistViewController *paylistVC = [[PaylistViewController alloc] init];
//        [self.navigationController pushViewController:paylistVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - payListAction End

#pragma mark - evaluateAction
- (IBAction)evaluateAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
        EvaluateViewController *evaluateVC = [[EvaluateViewController alloc] init];
        [self.navigationController pushViewController:evaluateVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - evaluateAction End

#pragma mark - allPayListAction
- (IBAction)allPayListAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
//        PaylistViewController *paylistVC = [[PaylistViewController alloc] init];
//        [self.navigationController pushViewController:paylistVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - allPayListAction End

#pragma mark - myCollectionAction
- (IBAction)myCollectionAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
        CollectionViewController *collectionVC = [[CollectionViewController alloc] init];
        [self.navigationController pushViewController:collectionVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - myCollectionAction End

#pragma mark - couponAction
- (IBAction)couponAction:(id)sender
{
    LoginStatus = [AccountManager isKiklinkLoggedin];
    if (LoginStatus == YES) {
//        CouponViewController *couponVC = [[CouponViewController alloc] init];
//        [self.navigationController pushViewController:couponVC animated:YES];
    } else {
        [AccountManager openLoginUI];
    }
}
#pragma mark - couponAction End

#pragma mark - JUMP
- (void)pushToUnpay
{
    UnpayViewController *unpayVC = [[UnpayViewController alloc] init];
    [self.navigationController pushViewController:unpayVC animated:YES];
}
- (void)pushToAllPaylist
{
    PaylistViewController *paylistVC = [[PaylistViewController alloc] init];
    [self.navigationController pushViewController:paylistVC animated:YES];
}
#pragma mark - JUMP End

@end
